#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int dx[3] = { -1, 0, 1 };
    int maxMoves(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size(), ret = 0;
        vector<vector<int>> dp(n, vector<int>(m, -1));
        for (int i = 0; i < n; i++) dp[i][0] = 0;
        for (int j = 0; j < m - 1; j++)
            for (int i = 0; i < n; i++) {
                if (dp[i][j] == -1) continue;
                for (int k = 0; k < 3; k++) {
                    int a = i + dx[k];
                    if (a < 0 || a >= n || grid[i][j] >= grid[a][j + 1]) continue;
                    ret = dp[a][j + 1] = j + 1;
                }
            }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) cout << dp[i][j] << " ";
            cout << endl;
        }
        return ret;
    }
};